package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.shiro.service.SysLoginService;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.security.PermitAll;
import java.util.Date;
import java.util.List;

@Api("微信调用")
@RestController
@RequestMapping("/weixin")
@PermitAll
public class WinXinController extends BaseController {
    @Autowired
    private SysLoginService loginService;
    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private IBizSendGasService bizSendGasService;
//    @Autowired
//    private IBizCheckService bizCheckService;

    @Autowired
    private IBizDicProblemService bizDicProblemService;

    @Autowired
    private IBizCheckTitleService bizCheckTitleService;

    @Autowired
    private ISysNoticeService noticeService;

    @Value("${ruoyi.hostUrl}")
    private String hostUrl;

    @ApiOperation("微信用户端登录")
    @PostMapping({"/login"})
    @ResponseBody
    public AjaxResult login(String username, String password) {
        SysUser user = null;

        try {
            user = this.loginService.login(username, password);
        } catch (Exception var5) {
            return AjaxResult.error("用户名或密码错误");
        }

        return AjaxResult.success(user);
    }

    @ApiOperation("上传1张图片")
    @PostMapping({"/upload"})
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file) throws Exception {
        try {
            String filePath = RuoYiConfig.getUploadPath();
            String fileName = FileUploadUtils.upload(filePath, file);
            //以前偌依默认上传url
            //String url = this.serverConfig.getUrl() + fileName;

            //修改后的url
            String url = hostUrl + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @ApiOperation("新增自检记录")
    @Log(
            title = "自检记录",
            businessType = BusinessType.INSERT
    )
    @PostMapping({"/send/add"})
    @ResponseBody
    public AjaxResult addSendSave(@RequestBody BizSendGasWinxin bizSendGasWinxin) {
        BizSendGas bizSendGas = new BizSendGas();
        bizSendGas.setShopName(bizSendGasWinxin.getShopName());
        bizSendGas.setShopPic(bizSendGasWinxin.getShopPic());
        bizSendGas.setShopSite(bizSendGasWinxin.getShopSite());
        bizSendGas.setSendPerson(bizSendGasWinxin.getSendPerson());
        bizSendGas.setGasPic(bizSendGasWinxin.getGasPic());
        bizSendGas.setAlarmPic(bizSendGasWinxin.getAlarmPic());
        bizSendGas.setGatePic(bizSendGasWinxin.getGatePic());
        bizSendGas.setIsFlow(bizSendGasWinxin.getIsFlow());
        bizSendGas.setInputTime(new Date());
        return this.toAjax(this.bizSendGasService.insertBizSendGas(bizSendGas));
    }

    @ApiOperation("按店铺名查询最近送气记录(多条)")
    @GetMapping({"/send/list"})
    @ResponseBody
    public AjaxResult getSend(String shopName) {
        BizSendGas bizSendGas = new BizSendGas();
        bizSendGas.setShopName(shopName);
        List<BizSendGas> list = this.bizSendGasService.selectBizSendGasList(bizSendGas);
        // return !list.isEmpty() ? AjaxResult.success(list.get(0)) : AjaxResult.success();
        return AjaxResult.success(list);
    }

//    @ApiOperation("新增检查记录-作废")
//    @Log(
//            title = "新增检查记录",
//            businessType = BusinessType.INSERT
//    )
//    @PostMapping({"/check/add"})
//    @ResponseBody
//    public AjaxResult addCheckSave(@RequestBody BizCheckWinxin bizCheckWinxin) {
//        BizCheck bizCheck = new BizCheck();
//        bizCheck.setShopName(bizCheckWinxin.getShopName());
//        bizCheck.setShopSite(bizCheckWinxin.getShopSite());
//        bizCheck.setCheckPerson(bizCheckWinxin.getCheckPerson());
//        bizCheck.setBadPic(bizCheckWinxin.getBadPic());
//        bizCheck.setSendPersonPhone(bizCheckWinxin.getSendPersonPhone());
//        bizCheck.setIsOk(bizCheckWinxin.getIsOk());
//        bizCheck.setBadRemark(bizCheckWinxin.getBadRemark());
//        bizCheck.setSendPerson(bizCheckWinxin.getSendPerson());
//        bizCheck.setIsRepair("0");
//        bizCheck.setCreateTime(new Date());
//        return this.toAjax(this.bizCheckService.insertBizCheck(bizCheck));
//    }

    @ApiOperation("新增检查记录")
    @PostMapping({"/checktitle/add"})
    @ResponseBody
    public AjaxResult addCheckTitle(@RequestBody BizCheckTitleWinxin bizCheckTitleWinxin) {
        BizCheckTitle bizCheckTitle = new BizCheckTitle();
        bizCheckTitle.setShopName(bizCheckTitleWinxin.getShopName());
        bizCheckTitle.setShopSite(bizCheckTitleWinxin.getShopSite());
        bizCheckTitle.setCheckPerson(bizCheckTitleWinxin.getCheckPerson());
        bizCheckTitle.setShopPic(bizCheckTitleWinxin.getShopPic());
        bizCheckTitle.setSendPersonPhone(bizCheckTitleWinxin.getSendPersonPhone());
        //bizCheckTitle.setIsOk(bizCheckWinxin.getIsOk());
        bizCheckTitle.setIsFlow(bizCheckTitleWinxin.getIsFlow());
        bizCheckTitle.setSendPerson(bizCheckTitleWinxin.getSendPerson());

        bizCheckTitle.setCreateTime(new Date());
        String isRepair = "0";
        for (BizCheckDetailWinxin bizCheckDetailWinxin : bizCheckTitleWinxin.getBizCheckDetailWinxinList()) {
            BizCheckDetail bizCheckDetail = new BizCheckDetail();
            bizCheckDetail.setProblemId(bizCheckDetailWinxin.getProblemId());
            bizCheckDetail.setProblem(bizCheckDetailWinxin.getProblem());
            bizCheckDetail.setIsChecked(bizCheckDetailWinxin.getIsChecked());
            if(bizCheckDetailWinxin.getIsChecked().equals("1")){
                isRepair = "1";
            }
        }
        bizCheckTitle.setIsRepair(isRepair);
        return this.toAjax(this.bizCheckTitleService.insertBizCheckTitle(bizCheckTitle));
    }

    @ApiOperation("整改记录，更新检查记录")
    @Log(
            title = "检查",
            businessType = BusinessType.UPDATE
    )
    @PostMapping({"/check/update"})
    @ResponseBody
    public AjaxResult updCheckResult(@RequestBody BizCheckRepairWinxin bizCheckRepairWinxin) {
        BizCheckTitle bizCheck = new BizCheckTitle();
        bizCheck.setId(bizCheckRepairWinxin.getId());
        bizCheck.setReppairPerson(bizCheckRepairWinxin.getRepairPerson());
        bizCheck.setRepairPic(bizCheckRepairWinxin.getRepairPic());
        bizCheck.setRepairTime(new Date());
        bizCheck.setIsRepair("1");
        return this.toAjax(this.bizCheckTitleService.updateBizCheckTitle(bizCheck));
    }

    @ApiOperation("某人待整改记录")
    @GetMapping({"/check/personlist"})
    @ResponseBody
    public TableDataInfo list(String personName) {
        BizCheckTitle bizCheck = new BizCheckTitle();
        bizCheck.setSendPerson(personName);
        //没整改
        bizCheck.setIsRepair("0");
        //有问题
        bizCheck.setIsOk("0");
        List<BizCheckTitle> list = this.bizCheckTitleService.selectBizCheckTitleList(bizCheck);
        return this.getDataTable(list);
    }

    @ApiOperation("问题字典")
    @GetMapping("/problem/list")
    @ResponseBody
    public TableDataInfo problemList()
    {
        BizDicProblem bizDicProblem = new BizDicProblem();
        List<BizDicProblem> list = bizDicProblemService.selectBizDicProblemList(bizDicProblem);
        return getDataTable(list);
    }

    @ApiOperation("通知公告")
    @GetMapping("/notice")
    @ResponseBody
    public AjaxResult list()
    {
        SysNotice notice=new SysNotice();
        notice.setStatus("0");
        startPage();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        if(!list.isEmpty()){
            return AjaxResult.success(list.get(0));
        }
        return  AjaxResult.success();
    }
}
